package create;

import Bean.CreateFileBean;
import Bean.FileBean;
import model.VmModel;
import util.AllUtil;
import util.Const;

import javax.annotation.Resource;

/**
 * Created by eleven on 15/12/30.
 */
public class CreateVm {

    @Resource(name = "allUtil")
    private AllUtil allUtil ;


    public Boolean Create(String name){
//        allUtil = new AllUtil();
        if (allUtil==null)
        {
            return false;
        }else {
            FileBean fileBean = new FileBean();
            fileBean.setFilename(name);
            fileBean.setFiletype("vm");
            return allUtil.createFile(fileBean);
        }
    }

    public Boolean Write(CreateFileBean createFileBean){

            VmModel vmModel = new VmModel();
        if (createFileBean.getChoice().equals("front")){
            FileBean fileBean = new FileBean();
            String text1=vmModel.CreatFront(createFileBean.getChinesename(),createFileBean.getDispatchname(),createFileBean.getVmpath());
            fileBean.setFilepath(Const.TEST_PATH + fileBean.getFilename() + ".vm" );
            return allUtil.writeFile(fileBean,text1);
        }else if (createFileBean.getChoice().equals("minor")){
            FileBean fileBean = new FileBean();
            String str="";
            String[] strbody = createFileBean.getDaogetname().split("，");
            for(String s:strbody){
                str=str+"                    <th>"+s+"</th>\n";
            };
                String str2="";
                for(String s:allUtil.readDaoFile(createFileBean.getDaopath())){
                        if (s.indexOf("sb.append")==-1||s.indexOf("getCreatetime")==-1){
                                if (s.indexOf("Ratio")!=-1){
                                        str2=str2+"                            <td>#set($r=$item."+s+"*100)$number.format(\"#0.00\", $r)%</td>\n";
                                }else if(s.indexOf("DayKey")!=-1){
                                        str2=str2+"                                <td><em class=\"font-bold\"> $date.format(\"yyyy-MM-dd\", $item."+s+")</em></td>\n";
                                }else{
                                        str2=str2+"                            <td>$number.number($item."+s+")</td>\n";
                                }
                        }
                };
            String text2=vmModel.CreatMinor(createFileBean.getChinesename(),createFileBean.getVmpath(),str,str2);
            return allUtil.writeFile(fileBean,text2);
        }else{
            return false;
        }
    }
}
